<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

class job extends entity {

	var $id;
	var $type;
	var $runid;
	var $runhash;
	var $extra;
	var $filler;
	var $statusid;
	var $started;
	var $userid;
	var $teamid;
	
    public function __construct()
    {
    }

	public function newInstance() {
		return new job;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".JOB." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function listAll() {
		$sql = "SELECT * FROM ".JOB;
		return $sql;
	}
	
	public function listBy() {
		$sql = "SELECT * FROM ".JOB;
		$filter="";
		if ($this->runhash!=null) {
			if ($filter!="") $filter.=" AND ";
			$filter .= addMFilter($this->runid,"runhash");
		}		
		if ($this->runid!=null) {
			if ($filter!="") $filter.=" AND ";
			$filter .= addMFilter($this->runid,"runid");
		}
		if ($this->type!=null) {
			$filter .= addMFilter($this->type,"type");
		}
		if ($this->statusid!=null) {
			if ($filter!="") $filter.=" AND ";
			$filter .= addMFilter($this->extra,"statusid");
		}
		if ($filter != "") $sql .= " WHERE " .$filter;
		return $sql;
	}

	public function insert() {
		if (!isset($this->runid)) $this->runid=0;
		$sql = "INSERT INTO ".JOB." (type, runid, runhash, extra, filler, statusid, userid, teamid) ";
		$sql .= "VALUES (";
		$sql .=	util::str($this->type);
		$sql .=	",".util::str($this->runid);
		$sql .=	",".util::str($this->runhash);
		$sql .=	",".util::str($this->extra);
		$sql .=	",".util::str($this->filler);
		$sql .=	",".util::str($this->statusid);
		$sql .=	",".util::str($this->userid);
		$sql .=	",".util::str($this->teamid);
		$sql .= ")";
		return $sql;
	}

	public function update() {
		$sql = "UPDATE ".JOB;
		$sql .= " set statusid=".util::str($this->statusid);
		$sql .= ", started = ".util::str($this->started)." WHERE id=".util::str($this->id);
		return $sql;
	}
	
	public function delete() {
		$sql = "DELETE FROM ".JOB. " WHERE id=".util::str($this->id);
		return $sql;
	}

	public function fill($array) {
		$this->id=$array['id'];
		$this->runid=$array['runid'];
		$this->runhash=$array['runhash'];
		$this->type=$array['type'];
		$this->extra=$array['extra'];
		$this->statusid=$array['statusid'];
		$this->filler=$array['filler'];
		$this->started=$array['started'];
		$this->userid=$array['userid'];
		$this->teamid=$array['teamid'];
	}
	
}

?>
